{% extends "layout.html" %}
{% block __data_browser__customized_head_block %}
  {{ super() }}
  <script type="text/javascript">
    $(function () {
      var products = {{ products|safe }};
      var selected_product_type = {{ task.product.product_type.id }};
      if (selected_product_type in products) {
        $.each(products[selected_product_type], function (idx, entry) {
          $("#product").append('<option value="' + entry.id + '">' + entry.name + '</option>');
        });
      }
      var product_id = {{ task.product.id }};
      $("#product").val(product_id).trigger("change");
      $("#product_type").change(function (ui) {
        var selected_product_type = $("#product_type").val();
        $("#product").empty();
        $.each(products[selected_product_type], function (idx, entry) {
          $("#product").append("<option value=" + entry.id + ">" + entry.name + "</option>");
        });
        $("#product").val(product_id);
        $("#product").select2({width: "resolve"});
      });
      $("#delete-button").click(function () {
        if (confirm("确定删除该卸货任务？")) {
          var form = $(this).parents("form");
          var input = $("<input type='hidden' name='action'></input>").val("delete");
          form.append(input);
          form.submit();
        } else {
          return false;
        }
      });
    });
  </script>
{% endblock %}

{% block body %}
  <div class="container">
    <div class="alert alert-info">
      车辆
      <strong>{{ task.unload_session.plate }}</strong>
      在卸货点
      <strong>{{ task.harbor.name }}</strong>
      完成了一次属于客户
      <strong>{{ task.customer.name }}</strong>
      的卸货任务
    </div>
    <form class="form-horizontal" method="POST">
      <legend>
        卸货任务详情
      </legend>
      <div class="form-group">
        <label for="customer" class="control-label col-lg-2">客户</label>

        <div class="col-lg-3">
          <select name="customer" id="customer" class="form-control">
            {% for customer in customers %}
              <option value="{{ customer.id }}" {% if customer.id == task.customer.id %}selected{% endif %}>
                {{ customer.name }}
              </option>
            {% endfor %}
          </select>
        </div>
      </div>

      <div class="form-group">
        <label for="product_type" class="control-label col-lg-2">产品类型</label>

        <div class="col-lg-3">
          <select id="product_type" name="product_type" class="form-control">
            {% for pt in product_types %}
              <option value="{{ pt.id }}" {% if pt.id == task.product.product_type.id %}selected="selected"{% endif %}>{{ pt.name }}</option>
            {% endfor %}
          </select>
        </div>
      </div>
      <div class="form-group">
        <label for="product" class="control-label col-lg-2">产品名称</label>

        <div class="col-lg-3">
          <select name="product" id="product" class="form-control"></select>
        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-lg-2">上次称重是否车上有人</label>

        <div class="col-lg-3">
          <p class="form-control-static">{{ task.unload_session.with_person_des }}</p>
        </div>
      </div>

      <div class="form-group">
        <label class="control-label col-lg-2">上次称重重量(公斤)</label>

        <div class="col-lg-3">
          <p class="form-control-static">{{ task.last_weight }}</p>
        </div>
      </div>

      <div class="form-group">
        <label for="weight" class="control-label col-lg-2">本次称重的重量为(公斤)</label>

        <div class="col-lg-3">
          <input type="number" name="weight" id="weight" size="20" required="True" min="1" max="{{ task.last_weight - 1 }}" class="form-control"
              {% if task.weight %}
                 value="{{ task.last_weight - task.weight }}"
              {% endif %} />
        </div>
      </div>
      <div class="form-group">
        <label for="pic-url" class="control-label col-lg-2">
          此次卸货的图片
        </label>

        <div class="col-lg-3">
          {% if task.pic_url %}
            <a class="fancybox thumbnail" href="{{ task.pic_url }}" rel="group" title="{{ task.product.name }}">
              <img class="img-responsive img-polaroid" src="{{ task.pic_url }}" alt="{{ task.product.name }}"/>
            </a>
          {% endif %}
        </div>
      </div>
      <input type="hidden" name="url" value="{{ request.args.get('url') or url_for('cargo.unload_session',id_=task.unload_session.id) }}">

      <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
          <button class="btn btn-lg btn-primary">
            <i class="fa fa-save fa-fw"></i>保存
          </button>
          <a class="btn btn-lg btn-default" href="{{ request.args.get("url") or url_for('cargo.unload_session',id_=task.unload_session.id) }}">
            <i class="fa fa-step-backward fa-fw"></i>返回
          </a>
          {% if not task.weight %}
            <button class="btn btn-lg btn-danger" type="button" id="delete-button">
              <i class="fa fa-times fa-fw"></i>删除
            </button>
          {% endif %}
        </div>
      </div>
    </form>
  </div>
{% endblock %}
